<?php

namespace app\shop\controller\api;

use app\ApiController;
use app\channel\model\ChannelGoodsModel;
use app\channel\model\ProxyUsersModel;
use app\favour\controller\sys_admin\FavourGoods;
use app\live\model\LiveGoodsModel;
use app\live\model\LiveRoomModel;
use app\member\model\UsersModel;
use app\shop\model\CartModel;
use app\shop\model\GoodsModel;
use app\weixin\model\MiniModel;
use app\shop\model\BonusModel;
use think\Db;
use app\mainadmin\model\TaskingModel;
use app\mainadmin\model\TaskingLogModel;

/*------------------------------------------------------ */
//-- 商品相关API
/*------------------------------------------------------ */

class Goods extends ApiController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new GoodsModel();
    }
    /*------------------------------------------------------ */
    //-- 获取商品列表
    /*------------------------------------------------------ */
    public function getList()
    {

        $this->Model->autoSale();//自动上下架处理
        $where[] = ['is_delete', '=', 0];
        $where[] = ['is_alone_sale', '=', 1];
        $where[] = ['isputaway', '=', 1];

        $search['keyword'] = input('keyword', '', 'trim');
        if (empty($search['keyword']) == false) {
            $where['and'][] = "( goods_name like '%" . $search['keyword'] . "%')  OR ( keywords like '%" . $search['keyword'] . "%')";
            #更新搜索记录
            $this->Model->updateRecord($this->userInfo, $search['keyword']);
        }

        $search['cateId'] = input('cateId', 0, 'intval');
        if ($search['cateId'] > 0) {
            $classList = $this->Model->getClassList();
            $where[] = ['cid', 'in', $classList[$search['cateId']]['children']];
        }
        $search['brand_id'] = input('brand_id', 0, 'intval');
        if ($search['brand_id'] > 0) {
            $where[] = ['brand_id', '=', $search['brand_id']];
        }

        $search['ids'] = input('ids', '', 'trim');
        if (empty($search['ids']) == false) {
            $where[] = ['goods_id', 'in', $search['ids']];
        }

        $sqlOrder = input('order', '', 'trim');
        $sort_by = strtoupper(input('sort', 'DESC', 'trim'));
        if (in_array(strtoupper($sort_by), array('DESC', 'ASC')) == false) {
            $sort_by = 'DESC';
        }
        switch ($sqlOrder) {
            case 'new':
                $this->sqlOrder = "is_new DESC,goods_id DESC";
                break;
            case 'sales':
                $this->sqlOrder = "virtual_sale $sort_by,goods_id DESC";
                break;
            case 'price':
                $this->sqlOrder = "shop_price $sort_by,goods_id DESC";
                break;
            default:
                $this->sqlOrder = "sort_order $sort_by,virtual_sale $sort_by,virtual_collect $sort_by,is_best $sort_by,goods_id DESC";
                break;
        }
        $data = $this->getPageList($this->Model, $where, 'goods_id,label_id', 10);
        $show_stock_num = settings('shop_goods_show_stock_num');
        $show_market_price = settings('shop_goods_show_market_price');
        foreach ($data['list'] as $key => $goods) {
            $_goods = [];
            $goods = $this->Model->info($goods['goods_id']);
            $_goods['goods_id'] = $goods['goods_id'];
            $_goods['goods_name'] = $goods['goods_name'];
            $_goods['short_name'] = $goods['short_name'];
            if (empty($_goods['short_name'])) {
                $_goods['short_name'] = $goods['goods_name'];
            }
            $_goods['goods_name'] = $goods['goods_name'];
            $_goods['is_spec'] = $goods['is_spec'];
            $_goods['exp_price'] = $goods['exp_price'];
            $_goods['sale_price'] = $goods['sale_price'];
            $_goods['now_price'] = $goods['sale_price'];
            $_goods['market_price'] = $goods['market_price'];
            $_goods['sale_count'] = $goods['sale_count'];
            $_goods['collect_count'] = $goods['collect_count'];
            $_goods['goods_thumb'] = $goods['goods_thumb'];
            $_goods['goods_number'] = $goods['goods_number'];
            $_goods['show_stock_num'] = $show_stock_num;
            $_goods['show_market_price'] = $show_market_price;
            // 商品标签
            $_goods['label'] = "";
            if ($goods['label_id']>0) {
                $label_info = Db::table("shop_label")->where('id',$goods['label_id'])->field('label')->find();
                $_goods['label'] = $label_info['label'];
            }
            $data['list'][$key] = $_goods;
        }
        return $this->success($data);
    }

    /*------------------------------------------------------ */
    //-- 更新搜索记录
    /*------------------------------------------------------ */
    public function searchLecords()
    {
        $search['keyword'] = input('keyword', '', 'trim');
        if (empty($search['keyword']) == false) {
            #更新搜索记录
            if (count($this->userInfo) > 0) {
                $this->Model->updateRecord($this->userInfo, $search['keyword']);
            }
        }
        return $this->success();
    }
    /*------------------------------------------------------ */
    //-- 更新搜索记录
    /*------------------------------------------------------ */
    public function searchLecordsList()
    {
            #更新搜索记录
            if (count($this->userInfo) > 0) {
                $where['user_id']=$this->userInfo['user_id'];
            $data_info=Db::name('user_goods_record')->where($where)->field('content')->order("update_time desc")->select();


        }
        return $this->success($data_info);
    }


    /*------------------------------------------------------ */
    //-- 获取商品信息
    /*------------------------------------------------------ */
    public function getGoodsInfo()
    {
        $goods_id = input('goods_id', 0, 'intval');
        $roomid = input('roomid', 0, 'intval');
        if ($goods_id < 1) {
            return $this->error('请传递商品ID.');
        }
        $data = $this->Model->info($goods_id);
        if (empty($data)) {
            return $this->error('没有找到相关商品.');
        }

        $data['show_sale_num'] = settings('shop_goods_show_sale_num');
        $data['show_collect_num'] = settings('shop_goods_show_collect_num');
        $data['show_market_price'] = settings('shop_goods_show_market_price');
        $data['show_stock_num'] = settings('shop_goods_show_stock_num');
        $data['shop_goods_comment'] = settings('shop_goods_comment');
        if ($data['is_credit_amount'] == 1){
            $data['amount_points'] = calcAmountPoints($data['credit_amount_points']);
        }else{
            $data['amount_points'] = 0;
        }

        $data['is_collect'] = 0;
        if (empty($this->userInfo['user_id']) == false) {//已登陆，查询用户是否有收藏此商品
            $data['is_collect'] = $this->Model->isCollect($goods_id, $this->userInfo['user_id']);
        }
        $data['is_cart'] = 1;
        $proxy_goods = (new ChannelGoodsModel())->info($goods_id);
        if (empty($proxy_goods) == false) {
            $data['is_cart'] = 0;
        }

        if ($roomid > 0){
            $roomGoods = (new LiveGoodsModel())->where('goods_id', $goods_id)->find();
            if (!empty($roomGoods)){
                $data['goods_number'] = $roomGoods['goods_num'];
                $data['sale_price'] = $roomGoods['price'];
                $data['min_price'] = $roomGoods['price'];
                $goods['sale_count'] = $data['virtual_sale'] + $roomGoods['sale_num'];
                $data['show_market_price'] = 0;
                if ($roomGoods['price_type'] == 3){
                    $data['sale_price'] = $roomGoods['price2'];
                    $data['min_price'] = $roomGoods['price2'];
                    $data['market_price'] = $roomGoods['price'];
                    $data['show_market_price'] = 1;
                }
            }
        }

        return $this->success($data);
    }



    /*------------------------------------------------------ */
    //-- 获取商品品牌列表
    /*------------------------------------------------------ */
    public function getBrandList()
    {
        $cid = input('cid', 0, 'intval');
        $return['list'] = $this->Model->getBrandList($cid);
        $return['code'] = 1;
        return $this->ajaxReturn($return);
    }



    /*------------------------------------------------------ */
    //-- 添加/取消收藏商品
    /*------------------------------------------------------ */
    public function collect()
    {
        $this->checkLogin();//验证登陆
        $goods_id = input('goods_id', 0, 'intval');
        if ($goods_id < 1) return $this->error('传参失败.');
        $GoodsCollectModel = new \app\shop\model\GoodsCollectModel();
        $where['goods_id'] = $goods_id;
        $where['user_id'] = $this->userInfo['user_id'];
        $collect = $GoodsCollectModel->where($where)->find();
        if (empty($collect) == false) {//存在,更新状态
            $upData['status'] = $collect['status'] == 1 ? 0 : 1;
            $upData['update_time'] = time();
            $res = $GoodsCollectModel->where($where)->update($upData);
        } else {
            $inData['status'] = 1;
            $inData['goods_id'] = $goods_id;
            $inData['user_id'] = $this->userInfo['user_id'];
            $inData['add_time'] = time();
            $inData['update_time'] = time();
            $res = $GoodsCollectModel->save($inData);
        }
        if ($res < 1) return $this->error('收藏商品失败.');
        return $this->success();
    }

    /*------------------------------------------------------ */
    //-- 获取商品收藏列表
    /*------------------------------------------------------ */
    public function getCollectlist()
    {
        $this->checkLogin();//验证登陆
        $GoodsCollectModel = new \app\shop\model\GoodsCollectModel();
        $where['user_id'] = $this->userInfo['user_id'];
        $where['status'] = 1;
        $rows = $GoodsCollectModel->where($where)->order('update_time DESC')->select();
        foreach ($rows as $row) {
            $goods = $this->Model->info($row['goods_id']);
            if ($goods['is_delete'] == 1) {
                continue;
            }
            $_goods['goods_id'] = $goods['goods_id'];
            $_goods['goods_name'] = $goods['goods_name'];
            $_goods['short_name'] = $goods['short_name'];
            $_goods['is_spec'] = $goods['is_spec'];
            $_goods['exp_price'] = $goods['exp_price'];
            $_goods['sale_price'] = $goods['sale_price'];
            $_goods['market_price'] = $goods['market_price'];
            $_goods['sale_count'] = $goods['sale_count'];
            $_goods['collect_count'] = $goods['collect_count'];
            $_goods['goods_thumb'] = $goods['goods_thumb'];
            $_goods['is_promote'] = $goods['is_promote'];
            $return['list'][] = $_goods;
            $return['count'] += 1;
        }
        return $this->success($return);
    }
    /*------------------------------------------------------ */
    //-- 获取商品详情
    /*------------------------------------------------------ */
    public function info()
    {
        $goods_id = input('id', 0, 'intval');
        if ($goods_id < 1) return $this->error('传参错误.');
        $goods = $this->Model->info($goods_id);
        $list['title'] = $goods['goods_name'];

        $web_path = config('config.host_path');
        $goods['m_goods_desc'] = preg_replace('/<img src=\"/', '<img style="width:100%;height:auto;" src="' . $web_path, $goods['m_goods_desc']);
        $list['goods'] = $goods;
        $list['imgsList'] = $this->Model->getImgsList($goods_id);
        $list['skuImgs'] = $this->Model->getImgsList($goods_id, true, true);
        $list['isCollect'] = $this->Model->isCollect($goods_id, $this->userInfo['user_id']);
        //获取sku图片

        //获取购物车信息
        $CartModel = new CartModel();
        $list['cartInfo'] = $CartModel->getCartInfo();

        $return['code'] = 1;
        $return['list'] = $list;
        return $this->ajaxReturn($return);
    }
    /*------------------------------------------------------ */
    //-- 获取搜索词，热搜之类的
    /*------------------------------------------------------ */
    public function get_keyword()
    {
        $return['default_keyword'] = settings('shop_index_search_text');
        $return['searchKeys'] = $this->Model->searchKeys();
        $return['hot_search'] = explode(' ', settings('hot_search'));
        $return['code'] = 1;
        return $this->ajaxReturn($return);
    }

    /*------------------------------------------------------ */
    //-- 检查商品活动
    /*------------------------------------------------------ */
    public function checkActivity()
    {
        if (class_exists('app\favour\model\FavourGoodsModel') == false) {
            return $this->success();
        }
        $goods_id = input('goods_id', 0, 'intval');
        $sku_id = input('sku_id', 0, 'intval');
        $goods = (new \app\favour\model\FavourGoodsModel)->checkIsFavour($goods_id, $sku_id);
        return $this->success($goods);
    }

    /*------------------------------------------------------ */
    //-- 商品分享页二维码
    /*------------------------------------------------------ */
    public function getShareImg()
    {
        $isnew = input('isnew', 0, 'intval');
        $is_wxmp = input('is_wxmp', 0, 'intval');
        $goods_id = input('goods_id', 0, 'intval');
        $file_name = $goods_id . '_' . $this->userInfo['user_id'] . '_' . $is_wxmp . '.jpg';
        $file_path = config('config._upload_') . 'goods_code/';
        if ($isnew == 0 && file_exists($file_path . $file_name) == true) {
            $res['img'] = trim($file_path . $file_name, '.');
            return $this->success($res);
        }
        /* $DividendShareByRole = settings('DividendShareByRole');
     if ($DividendShareByRole == 1 && $this->userInfo['role_id'] < 1) {
           $channel = (new ProxyUsersModel())->info($this->userInfo['user_id']);
           if ($channel['proxy_id'] < 1) {
               return $this->error('请升级身份后再操作.');
           }
           return $this->error('请升级身份后再操作.');
       }*/
        makeDir($file_path);
        $goods = $this->Model->info($goods_id);
        $settings = settings();
        $UsersModel = new UsersModel();
        $data['share_goods_bg'] = $settings['share_goods_bg'];
        $data['share_avatar'] = $UsersModel->getHeadImg($this->userInfo['headimgurl']);
        $data['share_nick_name'] = $this->userInfo['nick_name'];
        $data['share_qrcode'] = $is_wxmp == 0 ? $UsersModel->getMyQrCode($goods_id) : $UsersModel->getUserMiniQrcode($goods_id);

        $data['share_goods_name'] = $goods['goods_name'];
        $data['share_goods_price'] = '￥' . $goods['shop_price'];
        $data['share_goods_market_price'] = '￥' . $goods['market_price'];
        $data['share_goods_img'] = '.' . $goods['goods_img'];
        $data['share_goods_xy'] = $settings['share_goods_xy'];
        $data['share_goods_wh'] = $settings['share_goods_wh'];
        $data['share_goods_name_xy'] = $settings['share_goods_name_xy'];
        $data['share_goods_nickname_center'] = $settings['share_goods_nickname_center'];
        $data['share_goods_name_color'] = $settings['share_goods_name_color'];
        $data['share_goods_name_size'] = $settings['share_goods_name_size'];
        $data['share_goods_name_br'] = $settings['share_goods_name_br'];
        $data['share_goods_price_xy'] = $settings['share_goods_price_xy'];
        $data['share_goods_price_color'] = $settings['share_goods_price_color'];
        $data['share_goods_price_size'] = $settings['share_goods_price_size'];

        $data['share_goods_avatar_xy'] = $settings['share_goods_avatar_xy'];
        $data['share_goods_avatar_width'] = $settings['share_goods_avatar_width'];
        $data['share_goods_avatar_shape'] = $settings['share_goods_avatar_shape'];
        $data['share_goods_nickname_xy'] = $settings['share_goods_nickname_xy'];
        $data['share_goods_nickname_color'] = $settings['share_goods_nickname_color'];
        $data['share_goods_nickname_size'] = $settings['share_goods_nickname_size'];
        $data['share_goods_qrcode_xy'] = $settings['share_goods_qrcode_xy'];
        $data['share_goods_qrcode_width'] = $settings['share_goods_qrcode_width'];

        //扫一扫
        $data['share_goods_price_size_s'] = $settings['share_goods_price_size_s'];
        $data['share_goods_price_color_s'] = $settings['share_goods_price_color_s'];
        $data['share_goods_price_xy_s'] = $settings['share_goods_price_xy_s'];

        //市场价
        $data['share_goods_name_xy_s'] = $settings['share_goods_name_xy_s'];
        $data['share_goods_name_color_s'] = $settings['share_goods_name_color_s'];
        $data['share_goods_name_size_s'] = $settings['share_goods_name_size_s'];


        $MergeImg = new \lib\MergeImg();
        $MergeImg->shareGoodsImg($data, $file_path . $file_name);
        $res['img'] = trim($file_path . $file_name, '.');
        return $this->success($res);
    }


    /*------------------------------------------------------ */
    //-- 获取商品分类
    /*------------------------------------------------------ */
    public function getCateList()
    {
        $data['list'] = returnRecArr($this->Model->getClassList(false));
        return $this->success($data);
    }

    /*------------------------------------------------------ */
    //-- 分享商品获取积分
    /*------------------------------------------------------ */
    public function checkTasking() {
        $user_id = input('user_id', 0, 'intval');
        if ($user_id<=0) {
            return false;
        }
        $now_user_id = $user_id;
        $taskingLogModel = new TaskingLogModel();
        $taskingModel = new TaskingModel();
        $tasking_info = $taskingModel->where('id',4)->find();
        if ($tasking_info['status']==1) {
            Db::startTrans();
            if (!$taskingLogModel->where("status!=1 and tasking_id={$tasking_info['id']} and user_id={$now_user_id}")->update(['status'=>1]))  {
                $count_tasking = $taskingLogModel->where("tasking_id={$tasking_info['id']} and user_id={$now_user_id}")->count();
                if (($tasking_info['type']!=1||($tasking_info['type']==2&&$tasking_info['task_nums']<$count_tasking))&&!$taskingLogModel->insert(['user_id'=>$now_user_id,'tasking_id'=>$tasking_info['id'],'status'=>1,'createtime'=>time()])) {
                    Db::rollback();
                    return false;
                }
            }
            if ($tasking_info['type']!=1||($tasking_info['type']==2&&$tasking_info['task_nums']<$count_tasking)) {
                $userinfo_account = Db::table('users_account')->where('user_id', $now_user_id)->find();
                if (!Db::table('users_account')->where('user_id', $now_user_id)->udpate(['use_integral' => $userinfo_account['use_integral'] + $tasking_info['points']])) {
                    Db::rollback();
                    return false;
                }
            }
            Db::commit();
        }
        return true;
    }

    public function shareGoods(){
        (new TaskingModel())->shareGoods($this->userInfo['user_id']);

        return $this->success();
    }

}
